﻿using ComponentFactory.Krypton.Toolkit;
using StableDiffusionTools.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoRAExplorer.Forms
{
    public partial class TagExportDialog<T> : KryptonForm
    {
        public delegate void ResultRequestEventHandler(object sender, ResultRequestEventArgs meep);
        public class ResultRequestEventArgs
        {
            public ResultRequestEventArgs(T selectedEnum, bool sortDescending)
            {
                SelectedEnum = selectedEnum;
                SortDescending = sortDescending;
            }

            public T SelectedEnum { get; set; }
            public bool SortDescending { get; set; }
        }

        public event ResultRequestEventHandler ResultRequested;
        protected void OnResultRequested(ResultRequestEventArgs args)
        {
            ResultRequested?.Invoke(this, args);
        }

        public new string Text
        {
            get => kryptonTBInput.Text;
            set
            {
                kryptonTBInput.Text = value;
                //tbInput.Select(0, 0);
            }
        }

        private bool sortDescending = true;

        public TagExportDialog(string text, string caption)
        {
            InitializeComponent();

            Size newSize = TextRenderer.MeasureText(text, Font, new Size(1000, 1000), TextFormatFlags.Default);
            ClientSize = new Size(Math.Max(ClientSize.Width, newSize.Width), newSize.Height + ClientSize.Height);

            Preview.Text = text;

            if (!string.IsNullOrEmpty(caption))
                base.Text = caption;

            btnSorting.Image = sortDescending ? StableDiffusionTools.Properties.Resources.sort_descending32 : StableDiffusionTools.Properties.Resources.sort_ascending32;

            T[] enums = EnumUtil.GetValues<T>();
            kryptonCMBEnum.SelectedIndexChanged += (s, e) =>
            {
                OnResultRequested(new ResultRequestEventArgs(enums[kryptonCMBEnum.SelectedIndex], sortDescending));
            };

            for (int i = 0; i < enums.Length; i++)
            {
                kryptonCMBEnum.Items.Add(enums[i]);
            }

            btnSorting.Click += (s, e) =>
            {
                sortDescending = !sortDescending;
                btnSorting.Image = sortDescending ? StableDiffusionTools.Properties.Resources.sort_descending32 : StableDiffusionTools.Properties.Resources.sort_ascending32;
                OnResultRequested(new ResultRequestEventArgs(enums[kryptonCMBEnum.SelectedIndex], sortDescending));
            };

            KeyDown += (s, e) =>
            {
                if (e.KeyCode == Keys.Escape)
                {
                    DialogResult = DialogResult.Cancel;
                }
            };


            StartPosition = FormStartPosition.CenterParent;
        }

        public DialogResult ShowDialog(T selectedEnum, bool sortDescending) => ShowDialog(null, selectedEnum, sortDescending);
        public DialogResult ShowDialog(IWin32Window window, T selectedEnum, bool sortDescending)
        {
            if(this.sortDescending != sortDescending)
            {
                this.sortDescending = sortDescending;
                btnSorting.Image = sortDescending ? StableDiffusionTools.Properties.Resources.sort_descending32 : StableDiffusionTools.Properties.Resources.sort_ascending32;
            }
            kryptonCMBEnum.SelectedItem = selectedEnum;
            //enumComboBox1.PopulateFromEnum(selectedEnum);
            return base.ShowDialog(window);
        }

        //protected override void WndProc(ref Message m)
        //{
        //    const UInt32 WM_NCACTIVATE = 0x0086;

        //    if (m.Msg == WM_NCACTIVATE && m.WParam.ToInt32() == 0)
        //    {
        //        System.Diagnostics.Debug.WriteLine("WM_NCACTIVATE");
        //        DialogResult = DialogResult.Cancel;
        //    }
        //    else
        //    {
        //        base.WndProc(ref m);
        //    }
        //}

        [Obsolete("This is not supported in this class.", true)]
        public new void Show(IWin32Window window) { }
        [Obsolete("This is not supported in this class.", true)]
        public new void Show() { }
    }
}
